package chap04.Test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SessionServlet
 */
@WebServlet("/session")
public class SessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 // 获取当前会话

	    HttpSession session = request.getSession();

	    // 向 session 中保存 name 属性

	    session.setAttribute("name", "你的名字"); // 替换为你自己的名字

	    // 重定向到显示页面

	    response.sendRedirect("chap04/showSession.jsp");

	  }

	  @Override

	  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	    // 如果需要支持 POST 请求，可以在这里添加逻辑

	    doGet(request, response);

	  }
	

}
